package com.glut.controller;

import com.github.pagehelper.Page;
import com.glut.service.SstsUsersService;
import com.glut.vo.SecKld;
import com.glut.vo.SstsUsers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:沈林玉
 * @date:2020/2/5 11:21
 * @Description:
 */
@Controller
@RequestMapping("/users")
public class SstsUsersController {
    @Resource
    SstsUsersService sstsUsersService;

    /**
     * 1.学生用户登录
     * @param sstsUsers
     * @param session
     * @return
     */
    @RequestMapping("/studentLogin")
    public String studentLogin(SstsUsers sstsUsers, HttpSession session){
        SstsUsers user = sstsUsersService.login(sstsUsers);
        if (user!=null){
            session.setAttribute("loginUser",user);
        }
        return (user!=null?"redirect:/views/main.jsp":"redirect:/index.jsp");
    }

    /**
     * 2.查看学生账号
     * @param sstsUsers
     * @param
     * @return
     */
    @RequestMapping("/selectStudent/{pageNum}")
    public String selectStudent(SstsUsers sstsUsers,
                                @PathVariable Integer pageNum,
                                HttpSession session) {
        try{
            if(pageNum==null||pageNum< 1){
                // 保留查询条件
                pageNum=1;
            }
            Page<SstsUsers> user = (Page<SstsUsers>)sstsUsersService.selectStudent(sstsUsers,pageNum,10);
            session.setAttribute("selectStudent",user);
        }catch (Exception e){
            return "redirect:/error/500.jsp";
        }

        return "redirect:/views/user/user_list.jsp";
    }

    /**
     * 3.通过id查看学生账号
     * @param sstsUsers
     * @param
     * @return
     */
    @RequestMapping("/selectStudentById")
    public String selectStudentById(SstsUsers sstsUsers,HttpSession session){

        try{
            SstsUsers user = sstsUsersService.selectStudentById(sstsUsers);
            session.setAttribute("selectStudentById",user);
        }catch (Exception e){
            return "redirect:/error/500.jsp";
        }

        return "redirect:/views/user/updateForm.jsp";
    }

    /**
     * 3.教师新增学生账号
     * @param sstsUsers
     * @param
     * @return
     */
    @RequestMapping("/insertStudent")
    public ModelAndView insertStudent(SstsUsers sstsUsers){
        // 设置用户类型为0学生
        sstsUsers.setUserType(0);
        try{
            int insertStudent = sstsUsersService.insertStudent(sstsUsers);
            ModelAndView modelAndView=new ModelAndView("redirect:/users/selectStudent/0");
            return modelAndView;
        }catch (Exception e){
            ModelAndView modelAndView=new ModelAndView("redirect:/error/500.jsp");
            return modelAndView;
        }

    }

    /**
     * 4.教师更新学生账号
     * @param sstsUsers
     * @param
     * @return
     */
    @RequestMapping("/updateStudent")
    @ResponseBody
    public Map<String,Integer> updateStudent(SstsUsers sstsUsers){

        int updateStudent = sstsUsersService.updateStudent(sstsUsers);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("updateStudent",updateStudent);
        return map;
    }

    /**
     * 5.教师删除学生账号
     * @param sstsUsers
     * @param
     * @return
     */
    @RequestMapping("/deleteStudent")
    @ResponseBody
    public Map<String,Integer> deleteStudent(SstsUsers sstsUsers){

        int deleteStudent = sstsUsersService.deleteStudent(sstsUsers);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("deleteStudent",deleteStudent);
        return map;
    }


    /**
     * 4.教师用户登录
     * @param sstsUsers
     * @param session
     * @return
     */
    @RequestMapping("/teacherLogin")
    public String teacherLogin(SstsUsers sstsUsers, HttpSession session){
        SstsUsers user = sstsUsersService.login(sstsUsers);
        if (user!=null){
            session.setAttribute("loginUser",user);
        }
        return (user!=null?"redirect:/views/main.jsp":"redirect:/index.jsp");
    }

}
